﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;

namespace MSL.Utility
{
    /// <summary>
    /// 缓存操作类
    /// </summary>
    public class Cache
    {
        private readonly HttpContext H = new HttpContext(new System.Web.HttpRequest("Null.File", "http://cyq1162.cnblogs.com", String.Empty), new System.Web.HttpResponse(null));
        private System.Web.Caching.Cache _cache = null;

        private Cache()
        {
            this._cache = H.Cache;
        }
        /// <summary>
        /// 缓存时间[分钟]
        /// </summary>
        public Int32 CacheTime
        {
            get
            {
                int cacheTime = 0;
                int.TryParse(GetApp("CacheTime"), out cacheTime);
                return cacheTime;
            }
        }

        private String GetApp(string p)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static Cache Instance
        {
            get
            {
                return Shell.instance;
            }
        }
        class Shell
        {
            internal static readonly Cache instance = new Cache();
        }
        /// <summary>
        /// 是否存在缓存
        /// </summary>
        /// <param name="key">标识</param>
        /// <returns></returns>
        public bool Contains(string key)
        {
            return _cache != null && _cache[key] != null;
        }
        /// <summary>
        /// 获取数据缓存
        /// </summary>
        /// <param name="key">键</param>
        public object Get(string key)
        {
            if (Contains(key))
            {
                return _cache[key];
            }
            return null;
            //System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            //return objCache[key];
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        public void Add(string key, object obj)
        {
            HttpRuntime.Cache.Insert(key, obj);
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        public void Add(string key, object obj, TimeSpan Timeout)
        {
            HttpRuntime.Cache.Insert(key, obj, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        public void Add(string key, object obj, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            HttpRuntime.Cache.Insert(key, obj, null, absoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        public void RemoveAll(string key)
        {
            HttpRuntime.Cache.Remove(key);
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public void RemoveAll()
        {
            IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());
            }
        }
    }
}
